﻿using BCCommon;
using BCCommon.Extensions;
using BCCommon.TMS.Insured;
using BCData.TMS.InsuredConfig;
using BCData.TMS.VehicleTransportBusinessConfig;
using BCDto.Sim.Admin.Admin;
using BCDto.TMS.InsuredConfig;
using BCEntity.Common.EntityNameConst;
using BCEntity.TMS.InsuredConfig;
using BCService.Utils;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BCService.TMS.InsuredConfig
{
    public class InsuredConfigService : IInsuredConfigService
    {
        private readonly IInsuredConfigData insuredConfigData;
        private readonly IRedisService redisService;
        private readonly IVehicleTransportBusinessConfigData vehicleTransportBusinessConfigData;
        private readonly string key = string.Format("{0}", EntityNameConst.InsuredConfigEntity);

        public InsuredConfigService(IInsuredConfigData insuredConfigData, 
            IRedisService redisService, 
            IVehicleTransportBusinessConfigData vehicleTransportBusinessConfigData)
        {
            this.insuredConfigData = insuredConfigData;
            this.redisService = redisService;
            this.vehicleTransportBusinessConfigData = vehicleTransportBusinessConfigData;
        }

        public InsuredConfigDto Add(AdminDto admin, InsuredConfigRequestDto requestDto)
        {
            if (admin.CompanyId != 1)
            {
                throw new ArgumentException("您不是总公司员工，暂无权限添加");
            }
            var vehicleTransportBusinessConfig = this.vehicleTransportBusinessConfigData.GetEntity(requestDto.VehicleTransportBusinessConfigId).Result;
            if (vehicleTransportBusinessConfig == null)
            {
                throw new ArgumentException("车辆运输业务配置信息不存在");
            }
            if (requestDto.MinAmount > requestDto.MaxAmount)
            {
                throw new ArgumentException("最小金额不能大于最大金额");
            }
            var result = this.insuredConfigData.Insert(new InsuredConfigEntity()
            {
                InsuredType = requestDto.InsuredType.ToString(),
                InsuredTypeStr = requestDto.InsuredType.GetDescriptionString(),
                VehicleTransportBusinessConfigId = vehicleTransportBusinessConfig.VehicleTransportBusinessConfigId,
                MinAmount = requestDto.MinAmount,
                MaxAmount = requestDto.MaxAmount,
                InsuredFeeRatio = requestDto.InsuredFeeRatio,
                CreateAdminId = admin.AdminId,
                CreateAdminName = admin.FullName,
                CreateTime = DateTime.Now,
                UpdateTime = DateTime.Now,
                EditAdminId = admin.AdminId,
                EditAdminName = admin.FullName
            }).GetAwaiter().GetResult();
            this.redisService.DeleteMultipleKey(key, false);
            return result.As<InsuredConfigDto>();
        }

        public void Delete(AdminDto admin, int insuredConfigId)
        {
            if (admin.CompanyId != 1)
            {
                throw new ArgumentException("您不是总公司员工，暂无权限添加");
            }
            var result = this.insuredConfigData.GetEntity(insuredConfigId).Result;
            if (result == null)
            {
                throw new ArgumentException("此保价配置信息不存在");
            }
            this.insuredConfigData.Delete(insuredConfigId).GetAwaiter().GetResult();
            this.redisService.DeleteMultipleKey(key, false);
        }

        public InsuredConfigDto Get(int insuredConfigId)
        {
            string cacheKey = this.redisService.GetKey(key, insuredConfigId);
            var result = this.redisService.TryGet(cacheKey, () =>
            {
                var data = this.insuredConfigData.GetEntity(insuredConfigId).Result;
                return data.As<InsuredConfigViewDto>();
            }, TimeSpan.FromDays(30));
            return result;
           
        }

        public IEnumerable<InsuredConfigViewDto> GetList(AdminDto admin, InsuredType? insuredType, int? vehicleTransportBusinessConfigId)
        {
            string cacheKey = this.redisService.GetKey(key, insuredType, vehicleTransportBusinessConfigId);
            var result = this.redisService.TryGet(cacheKey, () =>
            {
                var data = this.insuredConfigData.GetList(insuredType, vehicleTransportBusinessConfigId).Result;
                return data.As<IEnumerable<InsuredConfigViewDto>>();
            }, TimeSpan.FromDays(30));
            return result;
        }

        public PageableList<InsuredConfigViewDto> Query(AdminDto admin, InsuredType? insuredType, int? vehicleTransportBusinessConfigId, DateTime? createStartDate, DateTime? createEndDate, int pageIndex, int pageSize)
        {
            string cacheKey = this.redisService.GetKey(key, insuredType, vehicleTransportBusinessConfigId, createStartDate, createEndDate, pageIndex, pageSize);
            var result = this.redisService.TryGet(cacheKey, () =>
            {
                var (data, dataCount) = this.insuredConfigData.Query(insuredType, vehicleTransportBusinessConfigId, createStartDate, createEndDate, pageIndex, pageSize).Result;
                return new PageableList<InsuredConfigViewDto>()
                {
                    Count = dataCount,
                    Items = data.As<List<InsuredConfigViewDto>>(),
                    PageIndex = pageIndex,
                    PageSize = pageSize
                };
            }, TimeSpan.FromDays(30));
            return result;
        }

        public InsuredConfigDto Update(AdminDto admin, int insuredConfigId, InsuredConfigRequestDto requestDto)
        {
            if (admin.CompanyId != 1)
            {
                throw new ArgumentException("您不是总公司员工，暂无权限修改");
            }
            if (requestDto.MinAmount > requestDto.MaxAmount)
            {
                throw new ArgumentException("最小金额不能大于最大金额");
            }
            var result = this.insuredConfigData.GetEntity(insuredConfigId).Result;
            if (result == null)
            {
                throw new ArgumentException("此保价配置信息不存在");
            }
            var vehicleTransportBusinessConfig = this.vehicleTransportBusinessConfigData.GetEntity(requestDto.VehicleTransportBusinessConfigId).Result;
            if (vehicleTransportBusinessConfig == null)
            {
                throw new ArgumentException("车辆运输业务配置信息不存在");
            }
            result.InsuredType = requestDto.InsuredType.ToString();
            result.InsuredTypeStr = requestDto.InsuredType.GetDescriptionString();
            result.VehicleTransportBusinessConfigId = vehicleTransportBusinessConfig.VehicleTransportBusinessConfigId;
            result.MinAmount = requestDto.MinAmount;
            result.MaxAmount = requestDto.MaxAmount;
            result.InsuredFeeRatio = requestDto.InsuredFeeRatio;
            result.UpdateTime = DateTime.Now;
            result.EditAdminId = admin.AdminId;
            result.EditAdminName = admin.FullName;
            result = this.insuredConfigData.Update(result).GetAwaiter().GetResult();
            this.redisService.DeleteMultipleKey(key, false);
            return result.As<InsuredConfigDto>();
        }
    }
}
